//#include "SimSPI.H"
//模拟SPI程序
#include "HardDriver.h"
//PB10->PWM_OUTPUT  TIM2_CH3  PB12->CSB	PB11->INT 	
//B13->SCK B14->MISO B15->MOSI

#define Sim_Spi_CE_H   GPIO_SetBits(GPIOB,GPIO_Pin_12) //LDC1000片选信号
#define Sim_Spi_CE_L   GPIO_ResetBits(GPIOB,GPIO_Pin_12) //LDC1000片选信号

#define Sim_Spi_IRQ_H  GPIO_SetBits(GPIOB,GPIO_Pin_11)  //IRQ主机数据输入
#define Sim_Spi_IRQ_L  GPIO_ResetBits(GPIOB,GPIO_Pin_11)  //IRQ主机数据输入

#define Sim_Spi_CS_H  GPIO_SetBits(GPIOB,GPIO_Pin_12) //SPI片选信号	   
#define Sim_Spi_CS_L  GPIO_ResetBits(GPIOB,GPIO_Pin_12) //SPI片选信号

#define Sim_Spi_SCK_H  GPIO_SetBits(GPIOB,GPIO_Pin_13)//SCK
#define Sim_Spi_SCK_L  GPIO_ResetBits(GPIOB,GPIO_Pin_13)//SCK

#define Sim_Spi_READ_MISO GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)//read MISO
#define Sim_Spi_MISO_H GPIO_SetBits(GPIOB,GPIO_Pin_14) //MISO
#define Sim_Spi_MISO_L GPIO_ResetBits(GPIOB,GPIO_Pin_14) //MISO

#define Sim_Spi_MOSI_H GPIO_SetBits(GPIOB,GPIO_Pin_15) //MOSI
#define Sim_Spi_MOSI_L GPIO_ResetBits(GPIOB,GPIO_Pin_15) //MOSI




void Sim_Spi_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

	//B13->SCK  
	//B15->MOSI  
	//PB12->CS
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;//
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;	   //SPI用复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);


	//PB11->INT 
	//B14->MISO
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_14;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_SetBits(GPIOB, GPIO_Pin_12 | GPIO_Pin_13  | GPIO_Pin_15);//
}



/************************************************************************/
/* 发送从机地址,addr=add+R/W
上升沿发送*/
/************************************************************************/

void Sim_Spi_SendAddress(uint8_t Addr)
{
	uint8_t i;
	Sim_Spi_CS_L;
	Sim_Spi_SCK_L;
	for (i=0;i<8;i++)
	{
		Sim_Spi_SCK_L;
		Delay_us(1);
		if ((Addr&0x80))
		{		
			Sim_Spi_MOSI_H;			
		}
		else
		{ 
			Sim_Spi_MOSI_L; 
		}
		Sim_Spi_SCK_H;
			Addr = Addr << 1;
			Delay_us(1);
	}
	
	Sim_Spi_SCK_L;
	Sim_Spi_MOSI_L;
	//Sim_Spi_CS_H;软件模拟的千万别发完从机地址后CS片选信号置高,尼玛数据在后面等时钟信号,还没传完!!!!

}

/************************************************************************/
/* 向从机发送数据,上升沿发送,
从从机接收数据,MSB在前*/
/************************************************************************/
uint8_t Sim_SpiSendData(uint8_t Data)
{
	uint8_t i;
	Sim_Spi_CS_L;
	Sim_Spi_SCK_L;
	for (i=0;i<8;i++)
	{
		Sim_Spi_SCK_L;
		Delay_us(1);
		if (Data&0x80)
		{
			Sim_Spi_MOSI_H;
		}
		else
		{
			Sim_Spi_MOSI_L;
		}
		Sim_Spi_SCK_H;
		Data = Data << 1;

		if (Sim_Spi_READ_MISO)
		{
			Data=Data+1;
		} 
		
		//
		Delay_us(1);

	}
	//Delay_us(1);
	Sim_Spi_SCK_L;
	Sim_Spi_MOSI_L;
	Sim_Spi_CS_H;
	return Data;
}
uint8_t Sim_Spi_ReadReg(uint8_t Addr)
{
	uint8_t Data;
	Addr=Addr|0x80;//LDC1000地址最高位1为读,0为写
	Sim_Spi_SendAddress(Addr);
	Data = Sim_SpiSendData(0x00);
	return Data;
}
uint8_t Sim_Spi_WriteReg(uint8_t Addr, uint8_t Data)
{
	uint8_t temp;
	Delay_us(1);
	Addr = Addr & 0x7F;//LDC1000地址最高位1为读,0为写
	Sim_Spi_SendAddress(Addr);
	temp = Sim_SpiSendData(Data);
	Delay_us(1);
	return temp;

}
/************************************************************************/
/*                                                                      */
/************************************************************************/
void Sim_Spi_ReadBytes(uint8_t Addr, uint8_t *Buf,uint8_t Len)
{
	while (Len > 0)
	{
		*Buf = Sim_Spi_ReadReg(Addr);
		Buf++;
		Addr++;
		Len--;
	}
	
}
